#!/usr/bin/env python
# coding: utf-8

# In[5]:


import csv
import pandas as pd
conStockData = pd.read_excel("2019年至今TOP5股票.xlsx",header = 1)   #读取成分股数据
stockCode = list(conStockData.columns)    #获取所有成分股股票代码，储存为列表
conStockDict = {"建筑装饰":stockCode[76:81],
                "轻工制造":stockCode[96:101],
                "机械设备":stockCode[56:61],
                "商业贸易":stockCode[101:106],
                "休闲服务":stockCode[116:121],
                "电气设备":stockCode[11:16]}    #将我们需要的板块名及对应股票代码存储为字典
def getUpPlateList(fileName):              #定义一个返回每日上涨板块名称列表的函数
    import csv
    with open(fileName,"r",encoding = "utf-8") as inFile:
        upPlateList = []
        csvReader = csv.reader(inFile)
        plateName = next(csvReader)
        for days in csvReader:
            dailyUpPlateList = [days[0]]
            for i in range(1,len(days)):
                if float(days[i]) == 1:
                    dailyUpPlateList.append(plateName[i])
            upPlateList.append(dailyUpPlateList)
        return upPlateList
upPlateList = getUpPlateList("Data_test.csv")      #用定义的函数返回每日上涨的板块名称列表
#使用策略，找到每日要买的股票代码，用“日期”-“股票代码列表”的形式存入字典
stockPurchasingDict = {}           
for days in range(len(upPlateList)):
    stockPurchasingList = []
    if "建筑装饰(申万)" in upPlateList[days] or "商业贸易(申万)" in upPlateList[days] or "电气设备(申万)" in upPlateList[days]:
        stockPurchasingList += conStockDict["轻工制造"]
    if "机械设备(申万)" in upPlateList[days] or "休闲服务(申万)" in upPlateList[days]:
        stockPurchasingList += conStockDict["商业贸易"]
    stockPurchasingDict[upPlateList[days][0]] = stockPurchasingList
stockPurchasingDF = pd.DataFrame(pd.Series(stockPurchasingDict),columns=["买入的股票代码"])    #将字典转化为Dataframe
stockPurchasingDF = stockPurchasingDF.reset_index().rename(columns={"index":"日期"})
stockPurchasingDF


# In[ ]:




